<?php
namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use app\common\lib\Show;
use app\admin\validate\Role as RoleValidate;
use think\exception\ValidateException;
use think\Exception;

/**
 * @Apidoc\Title("角色管理")
 * @Apidoc\Group("Role")
 * @Apidoc\Sort("6")
 */

class Role extends AdminBaseController
{
  /**
   * @Apidoc\Title("获取角色列表")
   * @Apidoc\Desc("获取角色列表接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/role/get_roles")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("列表")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="Role", desc="权限验证" )
   * @Apidoc\Param( ref="pagingParam")
   * @Apidoc\Returned("data", type="object", desc="业务数据",
   *    @Apidoc\Returned( ref="pagingParam"),
   *    @Apidoc\Returned("list", type="array",ref="app\common\model\Role\getRoleList", desc="数据列表")
   * )
   */
  public function getRoles(){
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    try {
      $qurey_info = $this->request->param();
      $result = $this->role_model->getRoleList($qurey_info, $this->super_user_auth);
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  /**
   * @Apidoc\Title("添加角色")
   * @Apidoc\Desc("添加角色接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/role/add_role")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("添加")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="AddRole", desc="权限验证" )
   * @Apidoc\Param("rolename", type="string",require=true, desc="角色名" )
   * @Apidoc\Param("nickname", type="string",require=true, desc="昵称" )
   * @Apidoc\Param("description", type="string",require=false, desc="描述" )
   * @Apidoc\Returned("data", type="boolean", desc="业务数据")
   */
  public function addRole()
  {
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    $qurey_info = $this->request->param();
    try {
      validate(RoleValidate::class)->scene('add')->check($qurey_info);
    }catch (ValidateException $validateException){
      return Show::error($validateException->getError());
    }
    $result = $this->role_model->getRoleByRolename($qurey_info['rolename']);
    if ($result) {
      return Show::error('角色已被使用');
    }
    try {
      $data = [
        'rolename' => $qurey_info['rolename'],
        'nickname' => $qurey_info['nickname'],
        'description' => $qurey_info['description']
      ];
      $result = $this->role_model->saveRole($data);
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  /**
   * @Apidoc\Title("编辑角色")
   * @Apidoc\Desc("编辑角色接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/role/edit_role")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("更新")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="EditRole", desc="权限验证" )
   * @Apidoc\Param("id", type="int",require=true, desc="角色ID" )
   * @Apidoc\Param("rolename", type="string",require=true, desc="角色名" )
   * @Apidoc\Param("nickname", type="string",require=true, desc="昵称" )
   * @Apidoc\Param("description", type="string",require=false, desc="描述" )
   * @Apidoc\Returned("data", type="int", desc="业务数据")
   */
  public function editRole()
  {
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    $qurey_info = $this->request->param();
    try {
      validate(RoleValidate::class)->scene('edit')->check($qurey_info);
    }catch (ValidateException $validateException){
      return Show::error($validateException->getError());
    }
    try {
      $data = [
        'id' => $qurey_info['id'],
        'rolename' => $qurey_info['rolename'],
        'nickname' => $qurey_info['nickname'],
        'description' => $qurey_info['description']
      ];
      $result = $this->role_model->updateRole($data);
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  /**
   * @Apidoc\Title("修改角色状态")
   * @Apidoc\Desc("修改角色状态接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/role/status_role")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("修改数据状态")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="DelRole", desc="权限验证" )
   * @Apidoc\Param("id", type="int",require=true, desc="角色ID" )
   * @Apidoc\Param("status", type="int",require=true, desc="状态：1（正常），0（禁用），-1（删除）" )
   * @Apidoc\Returned("data", type="int", desc="业务数据")
   */
  public function statusRole()
  {
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    $qurey_info = $this->request->param();
    try {
      $result = $this->role_model->statusRole($qurey_info);
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  /**
   * @Apidoc\Title("角色授权")
   * @Apidoc\Desc("角色授权接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/role/auth_role")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("更新")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="AuthRole", desc="权限验证" )
   * @Apidoc\Param("id", type="int",require=true, desc="角色ID" )
   * @Apidoc\Param("auths", type="string",require=true, desc="角色菜单menu_name组，逗号分割" )
   * @Apidoc\Returned("data", type="int", desc="业务数据")
   */
  public function authRole()
  {
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    $qurey_info = $this->request->param();
    try {
      $result = $this->role_model->authRole($qurey_info);
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
}